<?php
include ("includes/gen_password.php");

$query = "select * from tbl_reporting_personnel";
$result = mysql_query($query);

$cquery = "select * from  tbl_companies";
$cresult = mysql_query($cquery);


if($_POST['create'])
{
	$userlevel = $_POST['user_level'];
	$user = $_POST['rp_user_name'];
	$query = "SELECT COUNT(*) FROM tbl_reporting_personnel WHERE rp_username = '$user'";
	$data = mysql_query($query);
	$res = mysql_fetch_row($data);
	if($res[0] == 0)
	{
	$newPass = generatePassword();
	$newmd5Pass = md5($newPass);
	
	extract($_POST);
	$insert_query = "INSERT INTO tbl_reporting_personnel (rp_first_name,rp_last_name,rp_phone_number,c_nickname,rp_phone_ext,rp_fax_number,rp_email,rp_username,rp_password,user_level) " .
			"VALUES ('$rp_first_name','$rp_last_name','$rp_phone_number','$c_nickname','$rp_phone_ext','$rp_fax_number','$rp_email','$rp_user_name','$newmd5Pass','$userlevel');";
	mysql_query($insert_query);
	$subject = "AECA Reporting Portal";
    $body = "User Account for the AECA Reporting Portal with a user name of: ".$rp_user_name." has been created and the password is shown below:"."\n".$newPass."\n"."\n"."Please contact AECA if you continue to experience problems logging into your account.";
    if (mail($rp_email, $subject, $body)) {$success = 1;} else {$form = "nosend"; $success = 0;}
    if($success == 1){?>
    	<script>
				$(document).ready(function(){
					 $("#success").text("Username and Password has been sent successfully!").show();
					 window.location = "index.php?page=reporting-personnel";
				});
		</script>
    <?}
	}else{?>
		<script>
				$(document).ready(function(){
					 $("#error").text("Username Already Created..").show();
					 return false;
				});
				</script>
		<?}
}
?>
<style type="text/css">
.hide{
	display:none;
}
.show{
	display:block;
}
</style>
<script type="text/javascript" >
function opendialog(obj)
{
	openalertdialog(obj);
}
function openalertdialog(id)
{
	jQuery("#dialog").dialog("destroy");
	jQuery("#dialog").dialog({
			resizable: false,
			height:148,
			modal: true,
			buttons: {
				'Delete': function() {
					jQuery.post("/aec1/pages/delete_rpt_account.php",{id:id},function(data){
						jQuery('#rpt_del'+id).hide(function(){
								jQuery('#rpt_del'+id).remove();
						});
						$( "#dialog:ui-dialog" ).dialog( "destroy" );
						$( "#dialog-message" ).dialog({
						modal: true,
						buttons: {
							Ok: function() {
								$(this).dialog( "close" );
							}
						}
						});
					});
					
					jQuery(this).dialog('close');
				},
				Cancel: function() {
					jQuery(this).dialog('close');
				}
			}
		});
}
</script>
<script>
$(document).ready(function(){
	$("#username").change(function() { 
    	var usr = $("#username").val();
    	jQuery.post("/aec1/pages/user_availability.php",{user:usr},function(data){
						if(data == 1)
						{
							$("#availabilty").text("Username already exits").show().fadeOut(5000);
						}else{
							$("#availabilty").text("OK").show().fadeOut(5000);
						}
					});
    });
    $("#personal_info").validate();
});
  
function editpersonal(id){
  	$(".UserList"+name).removeClass( 'show').addClass('hide');
  	$.post("/aec1/pages/update_report_personnel.php",{uid:id},function(data){
  		if(data!=''){
  			$('.editUser').append(data);}
  	});
}
</script>
<!-- ui-dialog -->
<div id="dialog-message" title="Success Delete" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		Selected record has been deleted successfully.
	</p>
</div>
<div id="dialog" title="Dialog Title" style="display:none">
	<p>Are you sure, you want to delete the selected item?</p>
</div>

<span id='error'></span><span id='success'></span>		
<div id="tabs">
	<ul>
		<li><a href="#tabs-1">Edit Reporting Personnel</a></li>
		<li><a href="#tabs-2">Create New Personnel Account</a></li>
	</ul>
	<div id="tabs-1">
		<div class="UserList show">
		<table cellspacing='2' cellpadding='7'>
			<tr style="background-color:#0673C5;color:#FFFFFF;">
				<th>Id</th>
				<th>Name</th>
				<th>Company</th>
				<th>Email</th>
				<th>Username</th>
				<th>Actions</th>
			</tr>
			<? while($data = mysql_fetch_array($result)){?>
			<tr id="rpt_del<?=$data['rp_id'];?>" style="background-color:#E6E9F2;">
				<td><?=$data['rp_id'];?></td>
				<td><?=$data['rp_first_name'].' '.$data['rp_last_name'] ;?></td>
				<td><?=$data['c_nickname'];?></td>
				<td><?=$data['rp_email'];?></td>
				<td><?=$data['rp_username'];?></td>
				<td>
					<span class="ui-icon ui-icon-pencil" title="Edit" onclick="editpersonal(<?=$data['rp_id'];?>)" style="float:left;cursor:pointer"></span>
					<!-- <a href="index.php?page=edit-reporting-personnel&Id=<?=$data['rp_id'];?>" title="Edit"><div class="ui-icon ui-icon-pencil" style="float:left;"></div></a> -->
					<a href="javascript:void(0)" onclick="opendialog(<?=$data['rp_id'];?>)" class="ui-corner-all" title="Delete"><span class="ui-icon ui-icon-trash"></span></a>
				</td>
			</tr>
			<?}?>
		</table>
		</div>
		<div class="editUser"></div>
	</div>
	<div id="tabs-2">
		<form action="index.php?page=reporting-personnel" method="post" id="personal_info">
		<table cellpadding="4">
			<tr>
				<td><label><strong>First Name :</strong></label></td>
				<td><input type="text" name="rp_first_name" /></td>
			</tr>
			<tr>
				<td><label><strong>Last Name :</strong></label></td>
				<td><input type="text" name="rp_last_name" /></td>
			</tr>
			<tr>
				<td><label><strong>Company Name :</strong></label></td>
				<td>
				<select name="c_nickname">
				<? while($crow = mysql_fetch_array($cresult)){?>
					<option value=<?=$crow['c_nickname'];?>><?=$crow['c_nickname'];?></option>
				<?}?>
				</select>
				</td>
			</tr>
			<tr>
				<td><label><strong>Phone Number :</strong></label></td>
				<td><input type="text" name="rp_phone_number" class="number"/></td>
			</tr>
			<tr>
				<td><label><strong>Extension :</strong></label></td>
				<td><input type="text" name="rp_phone_ext" class="number"/></td>
			</tr>
			<tr>
				<td><label><strong>Fax Number :</strong></label></td>
				<td><input type="text" name="rp_fax_number" class="number"/></td>
			</tr>
			<tr>
				<td><label><strong>Email :</strong></label></td>
				<td><input type="text" name="rp_email" class="required email"/></td>
			</tr>
			<tr>
				<td><label><strong>Username :</strong></label></td>
				<td><input type="text" name="rp_user_name" id="username" class="required"/><span id="availabilty" style="color:red;padding-left:5px;"></span></td>
			</tr>
			<tr>
				<td><label><strong>User Level :</strong></label></td>
				<td>
				<select name="user_level">
					<option value="1">Read Only</option>
					<option value="2">Company Reporter</option>
					<option value="4">AECA Admin</option>
				</select>
				</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input type="submit" value="Create User" name="create" />
				<input type="button" value="Cancel" name="cancel" onclick="location.href='index.php?page=reporting-personnel'"/>
				</td>
			</tr>
		</table>
		</form>
	</div>
</div>